<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 可获得的最大点数 -->
    <script>
      var maxScore = function (cardPoints, k) {
        // 整体思路：环思维 + 滑动窗口
        const len = cardPoints.length
        const cards = [...cardPoints.slice(len - k), ...cardPoints.slice(0, k)]
        // 最大点数即：cards数组上长度为k的子数组的最大值
        let sum = cards.slice(0, k).reduce((prev, cur) => {
          return prev + cur
        })
        let ans = sum
        // 滑动窗口
        for (let i = 1; i <= k; i++) {
          sum = sum - cards[i - 1] + cards[i + k - 1]
          ans = Math.max(ans, sum)
        }
        return ans
      }
      console.log(maxScore([1, 2, 3, 4, 5, 6, 1], 3))
    </script>
  </body>
</html>
